﻿Public Class frmStudentEntry
    Dim dbBase As New dbStudent
    Dim dbSys As New dbSystem
    Dim dsBase As DataSet
    Dim frmCtrl As New control
    Dim curr As New currency
    Dim studentPicture As String
    Public EntryID As String
    Dim TypeAct As String

    Private Sub LoadData()
        dsBase = dbBase.GetStudentDetail(EntryID)
        Dim dtStudentDetail As DataTable = dsBase.Tables(0)
        Dim dtActiveCourse As DataTable = dsBase.Tables(1)
        Dim dtInactiveCourse As DataTable = dsBase.Tables(2)
        Dim dtActiveBill As DataTable = dsBase.Tables(3)
        Dim dtInactiveBill As DataTable = dsBase.Tables(4)
        txtGenID.Text = dtStudentDetail.Rows(0)("stdGenID").ToString()
        txtFullName.Text = dtStudentDetail.Rows(0)("stdFullName").ToString()
        txtAddress.Text = dtStudentDetail.Rows(0)("stdAddress").ToString()
        txtCity.Text = dtStudentDetail.Rows(0)("stdCity").ToString()
        txtPhoneM.Text = dtStudentDetail.Rows(0)("stdPhoneMobile").ToString()
        txtPhoneH.Text = dtStudentDetail.Rows(0)("stdPhoneHome").ToString()
        txtPhoneO.Text = dtStudentDetail.Rows(0)("stdPhoneOffice").ToString()
        Using ms As New IO.MemoryStream(DirectCast(dtStudentDetail.Rows(0)("Pict"), Byte()))
            If (ms.Length > 0) Then
                pbStudent.BackgroundImage = Image.FromStream(ms)
            End If
        End Using
        Dim rowCount As Integer = dtInactiveBill.Rows.Count
        If (rowCount > 0) Then
            dgPaidBill.Rows.Add(rowCount)
            For i = 0 To rowCount - 1
                Try
                    dgPaidBill.Rows(i).Cells(0).Value = dtInactiveBill.Rows(i)("sblGenID").ToString().Trim()
                    dgPaidBill.Rows(i).Cells(1).Value = CDate(dtInactiveBill.Rows(i)("sblCreateDate")).ToString("dd MMM yyyy")
                    dgPaidBill.Rows(i).Cells(2).Value = curr.dec2curr(CDec(dtInactiveBill.Rows(i)("sblAmount").ToString().Trim()))
                    dgPaidBill.Rows(i).Cells(3).Value = CDate(dtInactiveBill.Rows(i)("sblPayDate")).ToString("dd MMM yyyy")
                    dgPaidBill.Rows(i).Cells(4).Value = "Print"
                Catch ex As Exception
                End Try
            Next
        End If
        rowCount = dtActiveBill.Rows.Count
        If (rowCount > 0) Then
            dgUnpaidBill.Rows.Add(rowCount)
            For i = 0 To rowCount - 1
                Try
                    dgUnpaidBill.Rows(i).Cells(0).Value = dtActiveBill.Rows(i)("sblGenID").ToString().Trim()
                    dgUnpaidBill.Rows(i).Cells(1).Value = CDate(dtActiveBill.Rows(i)("sblCreateDate")).ToString("dd MMM yyyy")
                    dgUnpaidBill.Rows(i).Cells(2).Value = curr.dec2curr(CDec(dtActiveBill.Rows(i)("sblAmount").ToString().Trim()))
                    dgUnpaidBill.Rows(i).Cells(3).Value = CDate(dtActiveBill.Rows(i)("sblDueDate")).ToString("dd MMM yyyy")
                    dgUnpaidBill.Rows(i).Cells(4).Value = "Print"
                Catch ex As Exception
                End Try
            Next
        End If
    End Sub

    Private Sub frmStudentEntry_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        frmCtrl.reDesign(Me)
        Dim frm As New frmStudentList
        frm.ModuleName = "Administration"
        UcHeader1.Iniliaze("Management : Student Detail", Me, frm, Nothing, "", False)
        If (EntryID <> "") Then
            TypeAct = "U"
            LoadData()
            UcHeader1.ActionCtrl(True, False, False, True, True, True, False, False, False, True)
        Else
            TypeAct = "I"
            UcHeader1.ActionCtrl(False, False, False, False, False, True, False, False, False, True)
        End If
    End Sub

    ''================================== Control Header ==============================================''
    Private Sub tsNew_Click(ByVal sender As Object) Handles UcHeader1.tsNewClick
        Dim OpenForm As New frmStudentEntry
        OpenForm.EntryID = ""
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles UcHeader1.tsDeleteClick
        If (EntryID.Trim() <> "") Then
            TypeAct = "D"
            Dim result As String = True ''dbGL.deleteJournalGL(txtJournalID.Text, Var.CompID)
            If (result.IndexOf("success") > -1) Then
                Dim frm As New frmStudentList
                frm.ModuleName = "Administration"
                frmCtrl.openChild(frm, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Login User is not Exist")
        End If
    End Sub

    Private Sub tsSave_Click(ByVal sender As Object) Handles UcHeader1.tsSaveClick
        If (txtFullName.Text.Trim() <> "") Then
            Dim result As String = dbBase.StudentProfileManagement(TypeAct, txtGenID.Text.Trim(), txtFullName.Text.Trim(), txtAddress.Text.Trim(), _
                                                                   txtCity.Text.Trim(), txtPhoneM.Text.Trim(), txtPhoneH.Text.Trim(), txtPhoneO.Text.Trim(), _
                                                                   studentPicture, frmGlobal.LoginID)
            If (result.IndexOf("success") > -1) Then
                Dim frm As New frmStudentList
                frm.ModuleName = "Administration"
                frmCtrl.openChild(frm, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Fill All Field")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As Object) Handles UcHeader1.tsPrintClick
        ''Event 
        MessageBox.Show("Not included in prototype")
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles UcHeader1.tsRefreshClick
        Dim OpenForm As New frmStudentEntry
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As Object) Handles UcHeader1.tsCancelClick
        Dim OpenForm As New frmStudentEntry
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub
    ''================================== Control Header [End] ==============================================''

    Private Sub btnChangePicture_Click(sender As Object, e As EventArgs) Handles btnChangePicture.Click
        Dim dialog As New OpenFileDialog()
        If DialogResult.OK = dialog.ShowDialog Then
            studentPicture = dialog.FileName
            pbStudent.BackgroundImage = Image.FromFile(dialog.FileName)
        End If
    End Sub
End Class